package com.example.combwyaq.visualinterface.activity;


import android.Manifest;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;


import androidx.annotation.Nullable;

import com.example.combwyaq.R;
import com.example.combwyaq.handler.TimerHandler;
import com.example.combwyaq.mvp.contract.SplashContract;


import com.example.combwyaq.mvp.di.DaggerSplashComponent;
import com.example.combwyaq.mvp.di.SplashModules;
import com.example.combwyaq.mvp.presenter.SplashPresenter;
import com.example.combwyaq.mvp.viwe.BaseActivity;
import com.example.combwyaq.utils.SpUtils;
import com.example.combwyaq.thread.TimerThread;
import com.example.combwyaq.utils.LogUtils;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends BaseActivity<SplashPresenter> implements SplashContract.ISplashView , TimerHandler.TimerCallBack {

    private int time;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.CALL_PHONE,
                    Manifest.permission.INTERNET
            }, 101);
        }


    }

    @Override
    protected int addBarColor() {
        return Color.WHITE;
    }

    @Override
    public View addStatusView() {
        return f(R.id.splash_linear);
    }

    @Override
    public void callBack() {
        startPage(FragmentActivity.class);
        finish();
    }

    @Override
    public void refresh() {
        LogUtils.e("SplashPresenter->onNext");
        new TimerThread(new TimerHandler(this::callBack)).start();
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {

    }

    @Override
    public void inject() {
        DaggerSplashComponent.builder().splashModules(new SplashModules(this)).build()
                .injectSplash(this);
    }

    @Override
    public void initData() {
        String netcatch = SpUtils.readData("netcatch");
        if (netcatch!=null){
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    time++;
                    if (time>=3){
                        startPage(FragmentActivity.class);
                        timer.cancel();
                        finish();
                    }
                }
            },0,1000);

        }else{
            p.requestToken();
        }

    }


}
